#ifndef GVL_RESMAN_SHARED_H
#define GVL_RESMAN_SHARED_H

#include "../support/platform.hpp"

typedef struct gvl_shared_
{
	int ref_count;
	void (*freer)(void*);
} gvl_shared;

GVL_INLINE void gvl_release(gvl_shared* ptr)
{
	if(--ptr->ref_count == 0)
		ptr->freer(ptr);
}

void gvl_malloc_freer(void* ptr);

#define GVL_ADDREF(x) (++((gvl_shared*)(x))->ref_count)
#define GVL_RELEASE(x) gvl_release((gvl_shared*)(x))

GVL_INLINE void gvl_shared_init(gvl_shared* self, void (*freer)(void*))
{
	self->ref_count = 1;
	self->freer = freer;
}

#endif // GVL_RESMAN_SHARED_H
